package com.csscis.ssm.core.color;

import com.csscis.ssm.core.util.FileUtil;
import com.csscis.ssm.core.util.ImageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;

/**
 * Created by qinghe on 2016/3/28.
 */
public class PicAnalysis {
    private static final Logger logger = LoggerFactory.getLogger(PicAnalysis.class);

    /**
     * 返回水位检测状态
     * @return
     */
    public  static  boolean returnState() throws IOException {

        boolean flag = false;
        String photoPaht = "D:\\photo\\192.168.47.38\\";
        File dir = new File(photoPaht);

        String[] list = dir.list();
        if (list.length == 0) {
            return false;
        }else
        {
         String photo =   getNewPicPath(photoPaht);
            ImageHelper.cutImage(photo, "D:\\photo_cut\\1.jpg", 1542,988,10,10);
            ImageHelper.cutImage(photo, "D:\\photo_cut\\2.jpg", 1526,773,10,10);
            ImageHelper.cutImage(photo, "D:\\photo_cut\\3.jpg", 1526,553,10,10);

            int picCount = 0;
            int initR = 0;
            int initG = 0;
            int initB = 0;
            for (int i = 1; i <= 3; i++) {

                String path = "D:\\photo_cut\\" + i + ".jpg";
                BufferedImage img = ImageIO.read(new File(path));
                MMCQ.CMap result = MMCQ.computeMap(img, 10);
                Iterator<MMCQ.DenormalizedVBox> boxes = result.getBoxes().iterator();
                int flagR = 100;
                int flagG = 40;
                int flagB = 40;
                int pR = 0;
                int pG = 0;
                int pB = 0;
                int count = 0;
                while (boxes.hasNext()) {
                    count++;
                    MMCQ.DenormalizedVBox denormalizedVBox = (MMCQ.DenormalizedVBox) boxes.next();
                    int[] is = denormalizedVBox.getColor();

                    pR = pR + is[0];
                    pG = pG + is[1];
                    pB = pB + is[2];
                }
                //万一解不出来，赶紧跳出
                if(count == 0)
                    continue;
                pR = pR / count;
                pG = pG / count;
                pB = pB / count;
                if (picCount == 0) {
                    initR = pR;
                    initG = pG;
                    initB = pB;
                    picCount++;
                } else {
                    if (Math.abs(initR - pR) <= flagR &&
                            Math.abs(initG - pG) <= flagG &&
                            Math.abs(initB - pB) <= flagB) {
                        flag =false;
                        logger.info("===>No Warning ...photo"+photo);
                        logger.info("===>No Warning ..."+"initR:" + initR + " initG:" + initG+ " initB:" + initB);
                        logger.info("===>No Warning ..."+"PR:" + Math.abs(initR - pR) + " PG:" + Math.abs(initG - pG) + " PB:" + Math.abs(initB - pB));
                    } else {
                        logger.info("===>Warning ...photo"+photo);
                        logger.info("===>Warning ..."+"initR:" + initR + " initG:" + initG+ " initB:" + initB);
                        logger.info("===>Warning ..."+"PR:" + Math.abs(initR - pR) + " PG:" + Math.abs(initG - pG) + " PB:" + Math.abs(initB - pB));
                        flag = true;
                        return flag;
                    }
                }
            }
        }
        return flag;

    }

    /**
     * 得到最新的图片路径
     * @return
     */
    private static String getNewPicPath(String basePath){

        File path = new File(basePath);
        //列出该目录下所有文件和文件夹
        File[] files = path.listFiles();
        //按照文件最后修改日期倒序排序
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return (int) (file2.lastModified() - file1.lastModified());
            }
        });
        //取出第一个(即最新修改的)文件，打印文件名
        System.out.println(files[0].getName());

        FileUtil.deleteFile(basePath,files[0].getName());
        return basePath+files[0].getName();
    }


    public static void main(String[] args) throws IOException {
//        System.out.print();

    }
}
